import { Controller, Get, Post, Query, Req, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
import { Request } from 'express';
import { VisitorService } from './visitor.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { VisitorStatsDto, VisitorTrendDto, VisitorSourceDto, VisitorRecordDto, VisitorQueryDto } from './dto/visitor.dto';

@ApiTags('访客统计')
@Controller('visitors')
export class VisitorController {
  constructor(private readonly visitorService: VisitorService) {}

  @Get('stats')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({
    summary: '获取访客统计数据',
    description: '获取总访客数、今日访客数、平均停留时间和在线人数的统计信息'
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: VisitorStatsDto
  })
  @ApiResponse({ status: 401, description: '未授权' })
  async getStats() {
    return this.visitorService.getStats();
  }

  @Get('trend')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({
    summary: '获取访客趋势',
    description: '获取指定天数的访客数量趋势数据，默认为最近7天'
  })
  @ApiQuery({
    name: 'days',
    required: false,
    description: '要查询的天数',
    type: Number,
    example: 7
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: [VisitorTrendDto]
  })
  @ApiResponse({ status: 401, description: '未授权' })
  async getTrend(@Query('days') days: number) {
    return this.visitorService.getTrend(days);
  }

  @Get('sources')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({
    summary: '获取访客来源统计',
    description: '获取访客来源分布数据，包括直接访问、搜索引擎、社交媒体等'
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: [VisitorSourceDto]
  })
  @ApiResponse({ status: 401, description: '未授权' })
  async getSourceStats() {
    return this.visitorService.getSourceStats();
  }

  @Get('records')
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({
    summary: '获取访客记录',
    description: '获取访客详细记录，支持分页和日期范围查询'
  })
  @ApiQuery({
    name: 'startDate',
    required: false,
    description: '开始日期 (YYYY-MM-DD)',
    type: String,
    example: '2024-03-01'
  })
  @ApiQuery({
    name: 'endDate',
    required: false,
    description: '结束日期 (YYYY-MM-DD)',
    type: String,
    example: '2024-03-20'
  })
  @ApiQuery({
    name: 'page',
    required: false,
    description: '页码',
    type: Number,
    example: 1
  })
  @ApiQuery({
    name: 'limit',
    required: false,
    description: '每页数量',
    type: Number,
    example: 10
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    schema: {
      type: 'object',
      properties: {
        items: {
          type: 'array',
          items: { $ref: '#/components/schemas/VisitorRecordDto' }
        },
        total: {
          type: 'number',
          description: '总记录数'
        },
        page: {
          type: 'number',
          description: '当前页码'
        },
        limit: {
          type: 'number',
          description: '每页数量'
        }
      }
    }
  })
  @ApiResponse({ status: 401, description: '未授权' })
  async getRecords(@Query() query: VisitorQueryDto) {
    return this.visitorService.getRecords(query);
  }

  @Post('record-visit')
  @ApiOperation({
    summary: '记录访问',
    description: '记录新的访客访问信息，包括IP、地理位置、设备信息和来源'
  })
  @ApiResponse({
    status: 201,
    description: '记录成功',
    type: VisitorRecordDto
  })
  async recordVisit(@Req() request: Request) {
    const ip = request.ip || '未知IP';
    const userAgent = request.headers['user-agent'] || '未知设备';
    const referer = request.headers.referer || '直接访问';
    
    // 这里可以添加IP地理位置解析逻辑
    const location = '未知位置'; // 实际应用中需要通过IP解析获取
    
    return this.visitorService.recordVisit(ip, location, userAgent, referer);
  }

  @Post('record-leave')
  @ApiOperation({
    summary: '记录离开',
    description: '记录访客离开时间，用于计算访问时长'
  })
  @ApiResponse({
    status: 201,
    description: '记录成功'
  })
  async recordLeave(@Req() request: Request) {
    const ip = request.ip || '未知IP';
    return this.visitorService.recordLeave(ip);
  }
} 